package com.general.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.general.template.mapper.PowerUserMapper;
import com.general.template.utils.Pages;
import com.general.template.utils.StringUtils;
import com.general.template.vo.UserVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.general.template.pojo.PowerUser;
import com.general.template.service.PowerUserService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 杰哥
 * Date: 2021/2/15 16:52
 * @Description:
 **/

@Service
public class PowerUserServiceImpl extends ServiceImpl<PowerUserMapper, PowerUser> implements PowerUserService {


    /**
     * 添加用户
     *
     * @author 杰哥
     * @date 2021/2/16 15:25
     * @param user 添加的用户信息
     * @return int
     */
    @Override
    public int addUser(PowerUser user) {
        PowerUserMapper mapper = getBaseMapper();
        PowerUser phone = mapper.selectOne(new QueryWrapper<PowerUser>().eq("phone", user.getPhone()));
        if (phone != null) {
            return 0;
        } else {
            user.setHeaderUrl("http://localhost/header.png");
            user.setPassword(new Md5Hash(user.getPassword()).toString());
            user.setCreateTime(new Date());
            return mapper.insert(user);
        }
    }

    /**
     * 修改用户
     *
     * @author 杰哥
     * @date 2021/2/16 15:25
     * @param user 修改的用户信息
     * @return int
     */
    @Override
    public int updateUser(PowerUser user) {
        PowerUserMapper mapper = getBaseMapper();
        return mapper.updateById(user);
    }

    /**
     * 禁用用户
     *
     * @author 杰哥
     * @date 2021/2/16 15:25
     * @param id 禁用的用户id
     * @return int
     */
    @Override
    public int updateStatus(Integer id,Integer status) {
        PowerUserMapper mapper = getBaseMapper();
        PowerUser user = new PowerUser();
        user.setId(id);
        user.setStatus(status);
        return mapper.updateById(user);
    }

    /**
     * 批量导入用户
     *
     * @author 杰哥
     * @date 2021/2/16 15:25
     * @param list 导入用户信息列表
     * @return java.util.Map<java.lang.String,java.lang.Long>
     */
    @Override
    public Map<String, Long> addList(List<PowerUser> list) {
        Map<String, Long> map = new HashMap<>(16);
        long success = 0;
        long fail = 0;
        for (PowerUser user : list) {
            if (StringUtils.isNullOrEmpty(user.getName())||StringUtils.isNullOrEmpty(user.getPhone())){
                continue;
            }
            int flat = addUser(user);
            if (flat == 0) {
                fail++;
            } else {
                success++;
            }
        }
        map.put("success", success);
        map.put("fail", fail);
        return map;
    }

    /**
     * 分页查询用户
     *
     * @author 杰哥
     * @date 2021/2/16 15:26
     * @param pages 分页查询条件
     * @return com.general.template.utils.Pages<com.general.template.pojo.PowerUser>
     */
    @Override
    public Pages<PowerUser> pageList(Pages<PowerUser> pages) {
        PowerUserMapper mapper = getBaseMapper();
        IPage<PowerUser> page = new Page<>(pages.getCurrentPage(), pages.getPageSize());
        QueryWrapper<PowerUser> wrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> entry : pages.getParams().entrySet()) {
            if (!StringUtils.isNullOrEmpty(entry.getValue())){
                if (!"name".equals(entry.getKey())){
                    wrapper.eq(StringUtils.upperCharToUnderLine(entry.getKey()),entry.getValue());
                }else{
                    wrapper.like("name","%"+entry.getValue()+"%");
                }
            }
        }
        String desc = "desc";
        if (desc.equals(pages.getSortMethod())){
            wrapper.orderByDesc(StringUtils.upperCharToUnderLine(pages.getSortColumn()));
        }else{
            wrapper.orderByAsc(StringUtils.upperCharToUnderLine(pages.getSortColumn()));
        }
        IPage<PowerUser> selectPage = mapper.selectPage(page, wrapper);
        pages.setList(selectPage.getRecords());
        pages.setTotalCount((int) selectPage.getTotal());
        pages.setTotalPage((int) selectPage.getPages());
        return pages;
    }

    /**
     * 修改密码
     *
     * @author 杰哥
     * @date 2021/2/16 15:43
     * @param user
     * @return int
     */
    @Override
    public int updatePwd(UserVo user) {
        PowerUserMapper baseMapper = this.getBaseMapper();
        PowerUser one = baseMapper.selectOne(new QueryWrapper<PowerUser>().eq("phone", user.getPhone()).eq("password", user.getOldPassword()).eq("status", 0));
        if (one == null){
            return 0;
        }else{
            PowerUser powerUser = new PowerUser();
            powerUser.setId(user.getId());
            powerUser.setPassword(user.getPassword());
            return baseMapper.updateById(powerUser);
        }
    }
}


